home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
290_02
/
tblcmp.c
< prev
next >
Wrap
Text File
|
1990-05-12
|
47KB
|
1,607 lines
/* tblcmp - table compression routines */
/*
* Copyright (c) 1987, the University of California
*
* The United States Government has rights in this work pursuant to
* contract no. DE-AC03-76SF00098 between the United States Department of
* Energy and the University of California.
*
* This program may be redistributed. Enhancements and derivative works
* may be created provided the new works, if made available to the general
* public, are made available for use by anyone.
*/
#include "flexdef.h"
#include "main.h"
#include "misc.h"
#include "tblcmp.h"
#include "dfa.h"
#include "nfa.h"
#include "ecs.h"
extern int flexscan( void) ; /* from scan.c */
/* bldtbl - build table entries for dfa state
*
* synopsis
* int state[numecs], statenum, totaltrans, comstate, comfreq;
* bldtbl( state, statenum, totaltrans, comstate, comfreq );
*
* State is the statenum'th dfa state. It is indexed by equivalence class and
* gives the number of the state to enter for a given equivalence class.
* totaltrans is the total number of transitions out of the state. Comstate
* is that state which is the destination of the most transitions out of State.
* Comfreq is how many transitions there are out of State to Comstate.
*
* A note on terminology:
* "protos" are transition tables which have a high probability of
* either being redundant (a state processed later will have an identical
* transition table) or nearly redundant (a state processed later will have
* many of the same out-transitions). A "most recently used" queue of
* protos is kept around with the hope that most states will find a proto
* which is similar enough to be usable, and therefore compacting the
* output tables.
* "templates" are a special type of proto. If a transition table is
* homogeneous or nearly homogeneous (all transitions go to the same
* destination) then the odds are good that future states will also go
* to the same destination state on basically the same character set.
* These homogeneous states are so common when dealing with large rule
* sets that they merit special attention. If the transition table were
* simply made into a proto, then (typically) each subsequent, similar
* state will differ from the proto for two out-transitions. One of these
* out-transitions will be that character on which the proto does not go
* to the common destination, and one will be that character on which the
* state does not go to the common destination. Templates, on the other
* hand, go to the common state on EVERY transition character, and therefore
* cost only one difference.
*/
void bldtbl( state, statenum, totaltrans, comstate, comfreq )
int state[], statenum, totaltrans, comstate, comfreq;
{
int extptr, extrct[2][CSIZE + 1];
int mindiff, minprot, i, d;
int checkcom;
/* If extptr is 0 then the first array of extrct holds the result of the
* "best difference" to date, which is those transitions which occur in
* "state" but not in the proto which, to date, has the fewest differences
* between itself and "state". If extptr is 1 then the second array of
* extrct hold the best difference. The two arrays are toggled
* between so that the best difference to date can be kept around and
* also a difference just created by checking against a candidate "best"
* proto.
*/
extptr = 0;
/* if the state has too few out-transitions, don't bother trying to
* compact its tables
*/
if ( (totaltrans * 100) < (numecs * PROTO_SIZE_PERCENTAGE) )
mkentry( state, numecs, statenum, JAMSTATE, totaltrans );
else
{
/* checkcom is true if we should only check "state" against
* protos which have the same "comstate" value
*/
checkcom = comfreq * 100 > totaltrans * CHECK_COM_PERCENTAGE;
minprot = firstprot;
mindiff = totaltrans;
if ( checkcom )
{
/* find first proto which has the same "comstate" */
for ( i = firstprot; i != NIL; i = protnext[i] )
if ( protcomst[i] == comstate )
{
minprot = i;
mindiff = tbldiff( state, minprot, extrct[extptr] );
break;
}
}
else
{
/* since we've decided that the most common destination out
* of "state" does not occur with a high enough frequency,
* we set the "comstate" to zero, assuring that if this state
* is entered into the proto list, it will not be considered
* a template.
*/
comstate = 0;
if ( firstprot != NIL )
{
minprot = firstprot;
mindiff = tbldiff( state, minprot, extrct[extptr] );
}
}
/* we now have the first interesting proto in "minprot". If
* it matches within the tolerances set for the first proto,
* we don't want to bother scanning the rest of the proto list
* to see if we have any other reasonable matches.
*/
if ( mindiff * 100 > totaltrans * FIRST_MATCH_DIFF_PERCENTAGE )
{ /* not a good enough match. Scan the rest of the protos */
for ( i = minprot; i != NIL; i = protnext[i] )
{
d = tbldiff( state, i, extrct[1 - extptr] );
if ( d < mindiff )
{
extptr = 1 - extptr;
mindiff = d;
minprot = i;
}
}
}
/* check if the proto we've decided on as our best bet is close
* enough to the state we want to match to be usable
*/
if ( mindiff * 100 > totaltrans * ACCEPTABLE_DIFF_PERCENTAGE )
{
/* no good. If the state is homogeneous enough, we make a
* template out of it. Otherwise, we make a proto.
*/
if ( comfreq * 100 >= totaltrans * TEMPLATE_SAME_PERCENTAGE )
mktemplate( state, statenum, comstate );
else
{
mkprot( state, statenum, comstate );
mkentry( state, numecs, statenum, JAMSTATE, totaltrans );
}
}
else
{ /* use the proto */
mkentry( extrct[extptr], numecs, statenum,
prottbl[minprot], mindiff );
/* if this state was sufficiently different from the proto
* we built it from, make it, too, a proto
*/
if ( mindiff * 100 >= totaltrans * NEW_PROTO_DIFF_PERCENTAGE )
mkprot( state, statenum, comstate );
/* since mkprot added a new proto to the proto queue, it's possible
* that "minprot" is no longer on the proto queue (if it happened
* to have been the last entry, it would have been bumped off).
* If it's not there, then the new proto took its physical place
* (though logically the new proto is at the beginning of the
* queue), so in that case the following call will do nothing.
*/
mv2front( minprot );
}
}
}
/* cmptmps - compress template table entries
*
* synopsis
* cmptmps();
*
* template tables are compressed by using the 'template equivalence
* classes', which are collections of transition character equivalence
* classes which always appear together in templates - really meta-equivalence
* classes. until this point, the tables for templates have been stored
* up at the top end of the nxt array; they will now be compressed and have
* table entries made for them.
*/
void cmptmps()
{
int tmpstorage[CSIZE + 1];
register int *tmp = tmpstorage, i, j;
int totaltrans, trans;
peakpairs = numtemps * numecs + tblend;
if ( usemecs )
{
/* c